<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Dongasai\WkProtoPhp\Packets\RecvPacket;
use Dongasai\WkProtoPhp\Packets\SendPacket;
use Dongasai\WkProtoPhp\Packets\ConnectPacket;
use Dongasai\WkProtoPhp\Enums\Setting;
use Dongasai\WkProtoPhp\Enums\DeviceFlag;

/**
 * JSON转换功能使用示例
 */

// 1. 创建RecvPacket并转换为JSON
echo "=== RecvPacket JSON转换示例 ===\n";
$recvPacket = new RecvPacket();
$recvPacket->setSetting(new Setting(1));
$recvPacket->setMsgKey('test_msg_key_123456');
$recvPacket->setFromUid('user_123456');
$recvPacket->setChannelId('channel_001');
$recvPacket->setChannelType(1);
$recvPacket->setPayload('Hello, this is a received test message!');
$recvPacket->setMessageId(123456789);
$recvPacket->setMessageSeq(987654321);
$recvPacket->setTimestamp(1640995200);

// 转换为JSON
$json = $recvPacket->toJson();
echo "RecvPacket JSON:\n";
echo $json . "\n\n";

// 从JSON恢复
$restoredRecv = RecvPacket::fromJson($json);
echo "从JSON恢复的RecvPacket:\n";
echo "MessageID: " . $restoredRecv->getMessageId() . "\n";
echo "FromUID: " . $restoredRecv->getFromUid() . "\n";
echo "Payload: " . $restoredRecv->getPayload() . "\n\n";

// 2. 创建SendPacket并转换为JSON
echo "=== SendPacket JSON转换示例 ===\n";
$sendPacket = new SendPacket();
$sendPacket->setSetting(2);
$sendPacket->setMsgKey('send_msg_key_789012');
$sendPacket->setChannelId('channel_002');
$sendPacket->setChannelType(2);
$sendPacket->setPayload('This is a send message test!');
$sendPacket->setClientSeq(555666);
$sendPacket->setClientMsgNo('client_msg_789012');

// 转换为数组
$array = $sendPacket->toArray();
echo "SendPacket 数组:\n";
print_r($array);

// 转换为JSON
$json = $sendPacket->toJson();
echo "SendPacket JSON:\n";
echo $json . "\n\n";

// 从数组恢复
$restoredSend = SendPacket::fromArray($array);
echo "从数组恢复的SendPacket:\n";
echo "ClientSeq: " . $restoredSend->getClientSeq() . "\n";
echo "ClientMsgNo: " . $restoredSend->getClientMsgNo() . "\n";
echo "Payload: " . $restoredSend->getPayload() . "\n\n";

// 3. 创建ConnectPacket并转换为JSON
echo "=== ConnectPacket JSON转换示例 ===\n";
$connectPacket = new ConnectPacket();
$connectPacket->setVersion(3);
$connectPacket->setClientKey('client_key_abc123');
$connectPacket->setDeviceId('device_id_xyz789');
$connectPacket->setDeviceFlag(DeviceFlag::WEB->value);
$connectPacket->setClientTimestamp(1640995200);

// 转换为JSON
$json = $connectPacket->toJson();
echo "ConnectPacket JSON:\n";
echo $json . "\n\n";

// 从JSON恢复
$restoredConnect = ConnectPacket::fromJson($json);
echo "从JSON恢复的ConnectPacket:\n";
echo "Version: " . $restoredConnect->getVersion() . "\n";
echo "ClientKey: " . $restoredConnect->getClientKey() . "\n";
echo "DeviceId: " . $restoredConnect->getDeviceId() . "\n";
echo "DeviceFlag: " . $restoredConnect->getDeviceFlag() . "\n\n";

// 4. 实际应用场景示例
echo "=== 实际应用场景示例 ===\n";

// 模拟从网络接收到JSON数据
$receivedJson = '{"setting":1,"msgKey":"network_msg_key","fromUid":"network_user","channelId":"network_channel","channelType":1,"topic":"network_topic","payload":"Network message payload","messageId":999888777,"messageSeq":666555444,"timestamp":1640995200}';

// 直接从JSON创建Packet对象
$networkPacket = RecvPacket::fromJson($receivedJson);
echo "从网络JSON数据创建的RecvPacket:\n";
echo "FromUID: " . $networkPacket->getFromUid() . "\n";
echo "ChannelID: " . $networkPacket->getChannelId() . "\n";
echo "Topic: " . $networkPacket->getTopic() . "\n";
echo "Payload: " . $networkPacket->getPayload() . "\n";

// 处理后转换为JSON发送到其他系统
$processedJson = $networkPacket->toJson();
echo "\n处理后的JSON数据:\n";
echo $processedJson . "\n";

// 5. 批量处理示例
echo "\n=== 批量处理示例 ===\n";

$packets = [];
for ($i = 1; $i <= 3; $i++) {
    $packet = new RecvPacket();
    $packet->setSetting(new Setting($i));
    $packet->setMsgKey("batch_msg_key_$i");
    $packet->setFromUid("batch_user_$i");
    $packet->setChannelId("batch_channel_$i");
    $packet->setChannelType(1);
    $packet->setPayload("Batch message $i content");
    $packet->setMessageId($i * 1000);
    $packet->setMessageSeq($i * 100);
    $packet->setTimestamp(1640995200 + $i);
    
    $packets[] = $packet;
}

// 批量转换为JSON
$jsonArray = array_map(function($packet) {
    return json_decode($packet->toJson(), true);
}, $packets);

echo "批量JSON数据:\n";
echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";

// 批量从JSON恢复
$restoredPackets = array_map(function($jsonData) {
    return RecvPacket::fromJson(json_encode($jsonData));
}, $jsonArray);

echo "批量恢复的Packet数量: " . count($restoredPackets) . "\n";
foreach ($restoredPackets as $i => $packet) {
    echo "Packet " . ($i + 1) . ": " . $packet->getFromUid() . " -> " . $packet->getPayload() . "\n";
}

echo "\n=== JSON转换功能演示完成 ===\n";